@echo off
chcp 65001 >nul
echo 🚀 Starting Molecule Scope Application (Local Development)...
echo.

REM 设置本地开发环境变量
set BACKEND_PORT=8001
set FRONTEND_PORT=8002

REM 检查Python
echo 🔍 Checking Python installation...
python --version >nul 2>&1
if errorlevel 1 (
    python3 --version >nul 2>&1
    if errorlevel 1 (
        echo ❌ Python is not installed. Please install Python 3.8+
        pause
        exit /b 1
    )
)
echo ✅ Python is installed

REM 检查Node.js
echo 🔍 Checking Node.js installation...
node --version >nul 2>&1
if errorlevel 1 (
    echo ❌ Node.js is not installed. Please install Node.js 16+
    pause
    exit /b 1
)
echo ✅ Node.js is installed

REM 清理端口占用 - 使用更简单的方法
echo 🔍 Cleaning up port conflicts...
echo Attempting to free port %BACKEND_PORT%...
netstat -ano | findstr :%BACKEND_PORT% >nul
if not errorlevel 1 (
    echo Port %BACKEND_PORT% is in use, attempting to free it...
    for /f "tokens=5" %%a in ('netstat -ano ^| findstr :%BACKEND_PORT%') do (
        echo Found process PID %%a, attempting to terminate...
        taskkill /f /pid %%a >nul 2>&1 && echo Process terminated || echo Process may already be terminated
    )
    timeout /t 2 /nobreak >nul
)

echo Attempting to free port %FRONTEND_PORT%...
netstat -ano | findstr :%FRONTEND_PORT% >nul
if not errorlevel 1 (
    echo Port %FRONTEND_PORT% is in use, attempting to free it...
    for /f "tokens=5" %%a in ('netstat -ano ^| findstr :%FRONTEND_PORT%') do (
        echo Found process PID %%a, attempting to terminate...
        taskkill /f /pid %%a >nul 2>&1 && echo Process terminated || echo Process may already be terminated
    )
    timeout /t 2 /nobreak >nul
)

REM 安装Python依赖
echo 📦 Installing Python dependencies...
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
if errorlevel 1 (
    echo ❌ Failed to install Python dependencies
    pause
    exit /b 1
)
echo ✅ Python dependencies installed

REM 安装Node.js依赖
echo 📦 Installing Node.js dependencies...
cd client
npm install
if errorlevel 1 (
    echo ❌ Failed to install Node.js dependencies
    pause
    exit /b 1
)
cd ..
echo ✅ Node.js dependencies installed

REM 启动后端服务器
echo 🎯 Starting Backend Server (Port %BACKEND_PORT%)...
start "Molecule Scope Backend" cmd /k "python -m uvicorn main:app --reload --host 0.0.0.0 --port %BACKEND_PORT%"

REM 启动前端服务器
echo 🎯 Starting Frontend Server (Port %FRONTEND_PORT%)...
cd client
start "Molecule Scope Frontend" cmd /k "set VITE_FRONTEND_PORT=%FRONTEND_PORT% && echo Starting frontend on port %FRONTEND_PORT%... && npm run dev"
cd ..

echo.
echo ⏳ Waiting for servers to start (10 seconds)...
timeout /t 10 /nobreak >nul

echo.
echo 🌐 Application URLs:
echo Backend API:  http://localhost:8001
echo Frontend App: http://localhost:8002
echo API Docs:     http://localhost:8001/docs
echo.
echo 📋 Available API Endpoints:
echo   - GET  /health              - Health check
echo   - GET  /docs                - API documentation
echo   - GET  /materials/search    - Search materials
echo   - GET  /materials/{id}      - Get material details
echo   - GET  /materials/{id}/structure - Get crystal structure
echo   - POST /ai/chat             - AI chat interface
echo.
echo 🎉 Setup complete! Both servers are running.
echo.
echo 📝 Troubleshooting:
echo - If servers don't start, check the command windows for error messages
echo - If ports are busy, close other applications using ports 8001/8002
echo - If dependencies fail, run 'pip install -r requirements.txt' and 'cd client && npm install' manually
echo.
echo Press any key to exit this window (servers will continue running)...
pause >nul
